package com.yangtzeu.jwc.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMap.OnMapStatusChangeListener;
import com.baidu.mapapi.map.BaiduMap.OnMarkerClickListener;
import com.baidu.mapapi.map.BaiduMapOptions;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.yangtzeu.jwc.utils.G;
import com.zhangdi.jwc.R;
import com.zjsx.lib.utils.L;
import com.zjsx.lib.utils.T;

/**
 * 
 * @author ZhangDI
 * 
 */
public class MapActivity extends BaseActivity implements OnMarkerClickListener, OnMapStatusChangeListener {
    @SuppressWarnings("unused")
    private static final String LTAG = MapActivity.class.getSimpleName();
    private RelativeLayout map_container;
    private MapView mMapView;
    private BaiduMap mBaiduMap;
    // private PoiSearch mPoiSearch = null;
    GeoCoder mSearch = null; // 搜索模块，也可去掉地图模块独立使用
    LatLng lDongXiaoQu = new LatLng(30.338725d, 112.221965d);
    LatLng lXiXiaoQu = new LatLng(30.360580, 112.155913d);
    LatLng lWuHanXiaoQu = new LatLng(30.541562, 114.035240);
    LatLng lChengZhongXiaoQu = new LatLng(30.356014d, 112.192653d);
    TextView btn_dongxiaoqu, btn_xixiaoqu, btn_chengzhongxiaoqu, btn_wuhanxiaoqu;
    private ImageButton zoomInBtn, zoomOutBtn, my_location;

    // 定位相关
    LocationClient mLocClient;
    public MyLocationListenner myListener = new MyLocationListenner();
    BitmapDescriptor mCurrentMarker;
    boolean isFirstLoc = true;// 是否首次定位

    private SDKReceiver mReceiver;

    /**
     * 构造广播监听类，监听 SDK key 验证以及网络异常广播
     */
    public class SDKReceiver extends BroadcastReceiver {
	public void onReceive(Context context, Intent intent) {
	    String s = intent.getAction();
	    L.d(LTAG, "action: " + s);
	    if (s.equals(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR)) {
		L.e("key 验证出错! 请在 AndroidManifest.xml 文件中检查 key 设置");
	    } else if (s.equals(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR)) {
		L.e("网络出错");
	    } else {
		L.e("验证成功");
	    }
	}
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	toXiaoQu(lDongXiaoQu, R.drawable.icon_mark_dong, "东校区");

	// 注册 SDK 广播监听者
	IntentFilter iFilter = new IntentFilter();
	iFilter.addAction(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR);
	iFilter.addAction(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR);
	mReceiver = new SDKReceiver();
	registerReceiver(mReceiver, iFilter);

	float level = mBaiduMap.getMinZoomLevel() + 2;
	mBaiduMap.animateMapStatus(MapStatusUpdateFactory.zoomBy(level), 1000);
    }

    @Override
    public void setView() {
	super.setView();
	setContentView(R.layout.activity_map);
    }

    @Override
    public void initActionBar() {
	setSimpleActionBar(null, "长大地图", null, null);
    }

    @Override
    public void initView() {
	super.initView();
	map_container = (RelativeLayout) findViewById(R.id.map_container);
	// mMapView = (MapView) findViewById(R.id.mapView);
	zoomInBtn = (ImageButton) findViewById(R.id.zoomin);
	zoomOutBtn = (ImageButton) findViewById(R.id.zoomout);
	my_location = (ImageButton) findViewById(R.id.my_location);

	BaiduMapOptions bo = new BaiduMapOptions();
	bo.zoomControlsEnabled(false);
	mMapView = new MapView(this, bo);
	map_container.addView(mMapView, 0);
	mBaiduMap = mMapView.getMap();
	mBaiduMap.setOnMapStatusChangeListener(this);

	// 开启定位图层
	mBaiduMap.setMyLocationEnabled(true);
	// 定位初始化
	mLocClient = new LocationClient(this);
	mLocClient.registerLocationListener(myListener);
	LocationClientOption option = new LocationClientOption();
	option.setOpenGps(true);// 打开gps
	option.setCoorType("bd09ll"); // 设置坐标类型
	option.setScanSpan(1000);
	mLocClient.setLocOption(option);
	mLocClient.start();
	btn_dongxiaoqu = (TextView) findViewById(R.id.btn_dongxiaoqu);
	btn_xixiaoqu = (TextView) findViewById(R.id.btn_xixiaoqu);
	btn_chengzhongxiaoqu = (TextView) findViewById(R.id.btn_chengzhongxiaoqu);
	btn_wuhanxiaoqu = (TextView) findViewById(R.id.btn_wuhanxiaoqu);
    }

    @Override
    public void initListener() {
	super.initListener();
	btn_dongxiaoqu.setOnClickListener(this);
	btn_xixiaoqu.setOnClickListener(this);
	btn_chengzhongxiaoqu.setOnClickListener(this);
	btn_wuhanxiaoqu.setOnClickListener(this);
	zoomInBtn.setOnClickListener(this);
	zoomOutBtn.setOnClickListener(this);
	my_location.setOnClickListener(this);
    }

    public void toXiaoQu(LatLng ll, int drawable, String title) {
	mBaiduMap.clear();
	mBaiduMap.addOverlay(new MarkerOptions().position(ll).icon(BitmapDescriptorFactory.fromResource(drawable)).title(title));
	// mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(ll));
	MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
	mBaiduMap.setOnMarkerClickListener(this);
	mBaiduMap.animateMapStatus(u);
    }

    @Override
    protected void onPause() {
	super.onPause();
	// activity 暂停时同时暂停地图控件
	mMapView.onPause();
	mLocClient.stop();
    }

    @Override
    protected void onResume() {
	super.onResume();
	// activity 恢复时同时恢复地图控件
	mMapView.onResume();
	mLocClient.start();
    }

    @Override
    protected void onDestroy() {
	// 退出时销毁定位
	mLocClient.stop();
	// 关闭定位图层
	mBaiduMap.setMyLocationEnabled(false);
	mMapView.onDestroy();
	mMapView = null;
	super.onDestroy();
	// activity 销毁时同时销毁地图控件
	// 取消监听 SDK 广播
	unregisterReceiver(mReceiver);
    }

    @Override
    public void onClick(View v) {
	super.onClick(v);
	LatLng ll = null;
	int drawableId = -1;
	String title = null;
	switch (v.getId()) {
	case R.id.btn_dongxiaoqu:
	    ll = lDongXiaoQu;
	    drawableId = R.drawable.icon_mark_dong;
	    title = "东校区";
	    break;
	case R.id.btn_xixiaoqu:
	    ll = lXiXiaoQu;
	    drawableId = R.drawable.icon_mark_xi;
	    title = "西校区";
	    break;
	case R.id.btn_chengzhongxiaoqu:
	    ll = lChengZhongXiaoQu;
	    drawableId = R.drawable.icon_mark_zhong;
	    title = "城中校区";
	    break;
	case R.id.btn_wuhanxiaoqu:
	    ll = lWuHanXiaoQu;
	    drawableId = R.drawable.icon_mark_wu;
	    title = "武汉校区";
	    break;
	case R.id.zoomin:
	    float zoomLevel = mBaiduMap.getMapStatus().zoom;
	    if (zoomLevel <= 18) {
		// MapStatusUpdateFactory.zoomIn();
		mBaiduMap.animateMapStatus(MapStatusUpdateFactory.zoomIn());
		zoomOutBtn.setEnabled(true);
	    } else {
		T.showShort(this, "已经放至最大！");
		zoomInBtn.setEnabled(false);
	    }
	    break;
	case R.id.zoomout:
	    float zoomLevel2 = mBaiduMap.getMapStatus().zoom;
	    if (zoomLevel2 > 4) {
		mBaiduMap.animateMapStatus(MapStatusUpdateFactory.zoomOut());
		zoomInBtn.setEnabled(true);
	    } else {
		zoomOutBtn.setEnabled(false);
		T.showShort(this, "已经缩至最小！");
	    }
	    break;
	case R.id.my_location:
	    isFirstLoc = true;
	    if (!mLocClient.isStarted()) {
		mLocClient.start();
	    }
	    break;
	}
	if (ll != null) {
	    toXiaoQu(ll, drawableId, title);
	}
    }

    @Override
    public boolean onMarkerClick(Marker arg0) {
	T.showLong(this, arg0.getTitle());
	return false;
    }

    LatLng lastLl = null;
    boolean myl = false;
    LatLng myLl = null;

    /**
     * 定位SDK监听函数
     */
    public class MyLocationListenner implements BDLocationListener {

	@Override
	public void onReceiveLocation(BDLocation location) {
	   /* if (com.zjsx.lib.utils.G.DEBUG)
		T.showShort(MapActivity.this, location.getLatitude() + "--" + location.getLatitude());*/
	    // map view 销毁后不在处理新接收的位置
	    if (location == null || mMapView == null)
		return;
	    MyLocationData locData = new MyLocationData.Builder().accuracy(location.getRadius())
	    // 此处设置开发者获取到的方向信息，顺时针0-360
		    .direction(100).latitude(location.getLatitude()).longitude(location.getLongitude()).build();
	    mBaiduMap.setMyLocationData(locData);
	    lastLl = new LatLng(location.getLatitude(), location.getLongitude());
	    if (isFirstLoc) {
		isFirstLoc = false;
		myl = true;
		myLl = lastLl;
		MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(lastLl);
		mBaiduMap.animateMapStatus(u, 1000);
	    }
	}

	public void onReceivePoi(BDLocation poiLocation) {

	}
    }

    @Override
    public void onMapStatusChange(MapStatus arg0) {
	// TODO Auto-generated method stub

    }

    @Override
    public void onMapStatusChangeFinish(MapStatus arg0) {
	// TODO Auto-generated method stub
	if (myl) {
	    myl = false;
	    float level = mBaiduMap.getMinZoomLevel() + 4;
	    mBaiduMap.animateMapStatus(MapStatusUpdateFactory.zoomBy(level), 1000);
	}
    }

    @Override
    public void onMapStatusChangeStart(MapStatus arg0) {
	// TODO Auto-generated method stub

    }
}
